home *** CD-ROM | disk | FTP | other *** search
- /*
- * VirtuaLight's binary .VIB format API, sample 1
- * Written by Stephane Marty, 09/10/2001
- *
- * This sample program writes a binary VIB file
- * describing a simple procedural shell object.
- */
-
- #include "..\vlBinDef.h"
-
- #define MAXEPS (1.0e6)
-
- static int level;
- static double A, B, G, coeff, rfactor;
-
- void main(void)
- {
- unsigned long pri=0;
- viCAMERA *cam;
- viGENERAL *gen;
- viPOINT_LIGHT *pl;
- viFILE *vib;
- viSPHERE sphere;
- viDISK disk;
- viVECTOR Gv, Pv, vec;
- double r,angle;
- int i, steps;
-
- viSetVector(&Gv, -MAXEPS, -MAXEPS, -MAXEPS);
- viSetVector(&Pv, +MAXEPS, +MAXEPS, +MAXEPS);
- level = 2;
- A = 0.0;
- B = -1.5;
- G = 1.0;
- coeff = 0.15;
- rfactor = 1.0;
-
- // Open a new VIB file
- vib = viNewBinaryVIB("sample1.vib");
-
- // Create and declare the procedural object named "shell"
- viDeclareNewObject("shell", vib);
- steps = (int)(180.0 * pow(2.0, (double)level));
- for (i=-steps*2/3; i<=steps/3; ++i)
- {
- angle = 3.0 * 6.0 * M_PI * (double)i / (double)steps;
- r = rfactor * exp(coeff * angle);
- viSetDbl(sphere.center.x, r * sin(angle));
- viSetDbl(sphere.center.y, r * cos(angle));
- if (A > 0.0)
- viSetDbl(sphere.center.z, A * angle);
- else
- viSetDbl(sphere.center.z, B * r);
- viSetDbl(sphere.radius, r/G);
- // this shell is a simple set of spheres
- viDumpSphere(&sphere, vib);
- if (sphere.center.x > Gv.x) Gv.x = sphere.center.x;
- if (sphere.center.y > Gv.y) Gv.y = sphere.center.y;
- if (sphere.center.z > Gv.z) Gv.z = sphere.center.z;
- if (sphere.center.x < Pv.x) Pv.x = sphere.center.x;
- if (sphere.center.y < Pv.y) Pv.y = sphere.center.y;
- if (sphere.center.z < Pv.z) Pv.z = sphere.center.z;
- pri++;
- }
- viEndObjectDeclaration(vib);
-
- // Add the camera (low adaptive antialiasing enabled)
- cam = viNewCamera();
- viSetInt(cam->Format.X, 384);
- viSetInt(cam->Format.Y, 288);
- viSetVector(&cam->Location,
- (Gv.x+Pv.x)/2.0,
- ((Gv.y+Pv.y)/2.0+Gv.y)*1.1,
- Gv.z*5.0+10);
- viSetVector(&cam->LookAt,
- (Pv.x+Gv.x)/2.0,
- (Pv.y+Gv.y)/2.0,
- (Pv.z+Gv.z)/2.0);
- viSetVector(&cam->UpAxis, 0, 0, 1);
- viSetDbl(cam->FieldOfView, 60);
- viSetInt(cam->Antialiasing, 1);
- viDumpCamera(cam, vib);
-
- // Set the background color
- gen = viNewGeneral();
- viSetColor(&gen->Background, 0.4,0.4,0.5);
- viDumpGeneral(gen, vib);
-
- // Add a key pointlight
- pl = viNewPointLight();
- viSetColor(&pl->Intensity, 0.7,0.7,0.7);
- viSetVector(&pl->Position,
- cam->Location.x*8,
- cam->Location.y*3,
- 25);
- viDumpPointLight(pl, vib);
-
- // Add a fill pointlight (no shadow)
- pl = viNewPointLight();
- viSetColor(&pl->Intensity, 0.5,0.5,0.5);
- viSetVector(&pl->Position,
- -cam->Location.x*2,
- -cam->Location.y,
- 100);
- viSetByte(pl->LightingAttributes, LIGHT_ATTRIB_SPECULAR);
- viDumpPointLight(pl, vib);
-
- // Invoke the object (scaled down by 0.5)
- viCallObject("shell", vib);
- viObjectShaderName("shell_shader", vib);
- viDumpScale(viSetVector(&vec, 0.5, 0.5, 0.5), vib);
- viEndObjectCall(vib);
-
- // Add a simple disk for the floor (detached from the object)
- viPrimitive(vib);
- viSetVector(&disk.center, 0, 0, Pv.z);
- viSetVector(&disk.normal, 0, 0, 1);
- viSetDbl(disk.radius, 200);
- viDumpDisk(&disk, vib);
- viPrimitiveShaderName("ground", vib);
- viEndPrimitive(vib);
-
- // Close the VIB file
- viCloseBinaryVIB(vib);
-
- // Deallocate memory used
- free(cam);
- free(gen);
- free(pl);
-
- fprintf(stderr, "\n%lu spheres dumped.\n", pri);
- }